package com.epam.rentcar.DAO;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.naming.NamingException;

import com.epam.rentcar.transferobjects.Car;
import com.epam.rentcar.utils.dbconn.ConnEstablisher;
import com.epam.rentcar.utils.dbconn.ConnectionFactory;

public class CarDAOImpl implements CarDAO {

	ConnEstablisher connEstablisher;
	
	public CarDAOImpl(int wichConnection){
		connEstablisher = ConnectionFactory.getConnection(wichConnection);
	}
	
	public Car getCar(int carID) throws SQLException, NamingException {

		Car car = null;

		Connection connection = null;
		PreparedStatement pstmt = null;
		ResultSet resultSet = null;

		try {

			connection = connEstablisher.getConnection();

			pstmt = connection.prepareStatement("SELECT * FROM ADMIN.CARS WHERE ID=?");
			pstmt.setInt(1, carID);
			pstmt.execute();

			resultSet = pstmt.getResultSet();

			if (resultSet.next()) {
				car = new Car(
						resultSet.getInt("id"),
						resultSet.getString("manufacturer"),
						resultSet.getString("model"),
						resultSet.getString("number")
						);
			} 
		} 
		finally {
			connection.close();	
		}
		return car;	
	}

	public List<Car> getAvaliableCars() throws SQLException, NamingException {

		List<Car> cars = new ArrayList<Car>();

		Connection connection = null;
		PreparedStatement pstmt = null;
		ResultSet resultSet = null;

		try {

			connection = connEstablisher.getConnection();

			pstmt = connection.prepareStatement("" +
					"SELECT * FROM ADMIN.CARS WHERE AVALIABLE<>0");
			pstmt.execute();

			resultSet = pstmt.getResultSet();

			while (resultSet.next()) {
				cars.add(new Car(
						resultSet.getInt("id"),
						resultSet.getString("manufacturer"),
						resultSet.getString("model"),
						resultSet.getString("number")
						)) ;
			} 
		} finally {
			connection.close();		
		}
		return cars;			
	} 

	public boolean addCar(String manufacturer, String model, String number) throws SQLException, NamingException {

		Connection connection = null;
		PreparedStatement pstmt = null;

		try {

			connection = connEstablisher.getConnection();

			pstmt = connection.prepareStatement("INSERT INTO ADMIN.CARS (" +
					"MANUFACTURER, " +
					"MODEL, " +
					"NUMBER, " +
					"AVALIABLE) " +
					"VALUES (?,?,?,?)");
			pstmt.setString(1, manufacturer);
			pstmt.setString(2, model);
			pstmt.setString(3, number);
			pstmt.setInt(4, 1);

			return pstmt.execute();
		} finally {
			connection.close();	
		}
	}

	public boolean makeCarUnavaliable(int id) throws SQLException, NamingException {

		Connection connection = null;
		PreparedStatement pstmt = null;

		try {

			connection = connEstablisher.getConnection();

			pstmt = connection.prepareStatement(
					"UPDATE ADMIN.CARS SET AVALIABLE=0 " +
					"WHERE ID=?");
			pstmt.setInt(1, id);
			return pstmt.execute(); 
		} finally {
			connection.close();		
		}
	}

	public boolean makeCarAvaliable(int id) throws SQLException, NamingException {

		Connection connection = null;
		PreparedStatement pstmt = null;

		try {

			connection = connEstablisher.getConnection();

			pstmt = connection.prepareStatement(
					"UPDATE ADMIN.CARS SET AVALIABLE=1 " +
					"WHERE ID=?");
			pstmt.setInt(1, id);
			return pstmt.execute(); 
		} finally {
			connection.close();
		}
	}

}
